# -*- coding: utf-8 -*-
import pymongo
client = pymongo.MongoClient('127.0.0.1')
db = client.city4
import re
import MySQLdb
import MySQLdb.cursors
conn = MySQLdb.connect("127.0.0.1","root","123456789", "shantianjun",charset="utf8mb4",cursorclass=MySQLdb.cursors.DictCursor)
sql = "select * from girl"
cursor = conn.cursor()
cursor.execute(sql)
girl = cursor.fetchallDict()
data = {}
tmp ={}

for x in girl:
    tmp[x["url"]] = (x["id"],x["sid"])
datas = []
urlpool = set()
for y in db.movie.find():
        data = {}
        if y["url"] in urlpool:
            print "continue"+y["url"]
            continue
        # data["movie"] = y["movie"][0] if type(y["movie"]) == list else y["movie"]
        # if y.get("movie"):
        #     result = client.dmm.img.find_one({"type":"citymovie","source":x})
        #     if result:
        #         data["movie"] = result.get("path")
        # if y.get("thumb"):
        #     result = client.dmm.img.find_one({"type":"citymoviethumb","source":x})
        #     if result:
        #         data["thumb"] = result.get("path")
        data["thumb"] = y["thumb"]
        data["movie"] = y["movie"]
        if not tmp.get(y["girlUrl"]):
            print "not gid"+y["girlUrl"]
            continue
        data["gid"],data["sid"] = tmp.get(y["girlUrl"])
        print data
        datas.append((data["thumb"],data["gid"],data["movie"],data["sid"]))
        urlpool.add(y["url"])

sql = "insert into movie (thumb,gid,movie,sid) values (%s,%s,%s,%s)"
cursor.executemany(sql,datas)
cursor.close()
conn.commit()
# insert info cate (name) select select distinct(cate) from stores where cate <> ""
